package com.ruoyi.course.service.impl;

import java.util.List;

import com.doudou.core.container.ListUtil;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.CourseStudyRecordMapper;
import com.ruoyi.course.domain.CourseStudyRecord;
import com.ruoyi.course.service.ICourseStudyRecordService;

/**
 * course_recordService业务层处理
 *
 * @author zzx
 * @date 2023-04-05
 */
@Service
public class CourseStudyRecordServiceImpl implements ICourseStudyRecordService {
    @Autowired
    private CourseStudyRecordMapper courseStudyRecordMapper;

    /**
     * 查询course_record
     *
     * @param id course_record主键
     * @return course_record
     */
    @Override
    public CourseStudyRecord selectCourseStudyRecordById(Long id) {
        return courseStudyRecordMapper.selectCourseStudyRecordById(id);
    }

    /**
     * 查询course_record列表
     *
     * @param courseStudyRecord course_record
     * @return course_record
     */
    @Override
    public List<CourseStudyRecord> selectCourseStudyRecordList(CourseStudyRecord courseStudyRecord) {
        List<CourseStudyRecord> list = courseStudyRecordMapper.selectCourseStudyRecordList(courseStudyRecord);
        if (ListUtil.isNotEmpty(list)){
            for (CourseStudyRecord record : list) {
                //检查课程学习记录，如果学习记录数大于了课件总数，就等于课件总数
                Integer newTotal = record.resetStudyTotal();
                if (newTotal != null){
                    CourseStudyRecord build = CourseStudyRecord.builder().id(record.getId()).studyTotal(newTotal).build();
                    updateCourseStudyRecord(build);
                }
            }
        }
        return list;
    }

    /**
     * 新增course_record
     *
     * @param courseStudyRecord course_record
     * @return 结果
     */
    @Override
    public int insertCourseStudyRecord(CourseStudyRecord courseStudyRecord) {
        courseStudyRecord.setCreateTime(DateUtils.getNowDate());
        return courseStudyRecordMapper.insertCourseStudyRecord(courseStudyRecord);
    }

    /**
     * 修改course_record
     *
     * @param courseStudyRecord course_record
     * @return 结果
     */
    @Override
    public int updateCourseStudyRecord(CourseStudyRecord courseStudyRecord) {
        return courseStudyRecordMapper.updateCourseStudyRecord(courseStudyRecord);
    }

    /**
     * 批量删除course_record
     *
     * @param ids 需要删除的course_record主键
     * @return 结果
     */
    @Override
    public int deleteCourseStudyRecordByIds(Long[] ids) {
        return courseStudyRecordMapper.deleteCourseStudyRecordByIds(ids);
    }

    /**
     * 删除course_record信息
     *
     * @param id course_record主键
     * @return 结果
     */
    @Override
    public int deleteCourseStudyRecordById(Long id) {
        return courseStudyRecordMapper.deleteCourseStudyRecordById(id);
    }

    @Override
    public CourseStudyRecord selectCourseStudyRecordByAllId(CourseStudyRecord courseStudyRecord) {
        return courseStudyRecordMapper.selectCourseStudyRecordByAllId(courseStudyRecord);
    }
}
